package com.dyz.leetcode._02_string;

public class _6_convert {
    public String convert(String s, int numRows) {
        if (s==null || s.length()<=numRows){return s;}
        char[] chars = s.toCharArray();
        int n  = chars.length;

        StringBuilder[] sb = new StringBuilder[numRows];
        //初始化sb
        for(int i=0; i<numRows; i++){
            sb[i] = new StringBuilder();
        }

        for(int i = 0; i<n;){
            //上到下
            for(int row=0; row<numRows&& i<n; row++){
                sb[row].append(chars[i]);
                i++;
            }
            // 下到上
            for(int row=numRows-2; row>0&& i<n; row--){
                sb[row].append(chars[i]);
                i++;
            }
        }
        StringBuilder res = new StringBuilder();
        for(StringBuilder ss:sb){
            res.append(ss);

        }
        return res.toString();
    }

    // 按行访问
    public String convert1(String s, int numRows) {
        if (numRows == 1) return s;

        StringBuilder sb = new StringBuilder();
        int delta = 2 * numRows - 2;

        int n = s.length();

        for (int row = 0; row < numRows; row++) {
            for (int col = 0; col + row < n; col += delta) {
                sb.append(s.charAt(col + row));
                if (row != 0 && row != numRows - 1 && col + delta - row < n) {
                    sb.append(s.charAt(col + delta - row));
                }
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        new _6_convert().convert("asdadfaacfsd", 3);
    }
}
